/*
  File:    TumorSaveUtilities.cpp
  Purpose: Routines to save the current tumor data at both regular intervals
           and for resume operations.  

  Contact: Paul Macklin
           pmacklin@math.uci.edu
		  http://math.uci.edu/~pmacklin
*/

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <iostream>
#include <fstream>

using namespace std;

#include "../Matrix.h"
#include "Curvature.h"
#include "../FileNames.h"

bool WriteTumorConfigurationFile( double time )
{
 extern char SaveDirectory [1025];
 extern char* StringBuffer;
 
 extern double TumorOxygenUptakeRate;
 extern double QuiescentOxygenUptakeRate;
 extern double NecroticOxygenUptakeRate;
 extern double ECMoxygenUptakeRate;
 extern double QuiescenceOxygenLevel;
 extern double NecrosisOxygenLevel;
 extern double ApoptosisRate;
 extern double TumorAggressivenessG;
 extern double TumorNecrosisRateGN;

 ConstructFilename( StringBuffer, SaveDirectory,"Parameters",time,"cfg" );   
 
 fstream OutputFile( StringBuffer, ios::out );
 OutputFile << "TumorOxygenUptakeRate = " << TumorOxygenUptakeRate << endl;
 OutputFile << "QuiescentOxygenUptakeRate = " << QuiescentOxygenUptakeRate << endl;
 OutputFile << "NecroticOxygenUptakeRate = " << NecroticOxygenUptakeRate << endl;
 OutputFile << "ECMoxygenUptakeRate = " << ECMoxygenUptakeRate << endl << endl;

 OutputFile << "QuiescenceOxygenLevel = " << QuiescenceOxygenLevel << endl;
 OutputFile << "NecrosisOxygenLevel = " << NecrosisOxygenLevel << endl;
 
// OutputFile << "MaxOxygenDeliveryRate = " << MaxOxygenDeliveryRate << endl << endl;
 
 OutputFile << "ApoptosisRate = " << ApoptosisRate << endl;
 OutputFile << "TumorAggressivenessG = " << TumorAggressivenessG << endl;
 OutputFile << "TumorNecrosisRateGN = " << TumorNecrosisRateGN << endl << endl;
 
 OutputFile << "time = " << time << endl << endl;

 OutputFile.close();
 
 return true;
}

bool WriteResumeTumorConfigurationFile( void )
{
 extern char SaveDirectory [1025];
 extern char* StringBuffer;
 extern char TumorParameterFile [1025];
 
 extern double t;
 
 extern double TumorOxygenUptakeRate;
 extern double QuiescentOxygenUptakeRate;
 extern double NecroticOxygenUptakeRate;
 extern double ECMoxygenUptakeRate;
 extern double QuiescenceOxygenLevel;
 extern double NecrosisOxygenLevel;
 extern double ApoptosisRate;
 extern double TumorAggressivenessG;
 extern double TumorNecrosisRateGN;

 fstream OutputFile( TumorParameterFile, ios::out );
 OutputFile << "TumorOxygenUptakeRate = " << TumorOxygenUptakeRate << endl;
 OutputFile << "QuiescentOxygenUptakeRate = " << QuiescentOxygenUptakeRate << endl;
 OutputFile << "NecroticOxygenUptakeRate = " << NecroticOxygenUptakeRate << endl;
 OutputFile << "ECMoxygenUptakeRate = " << ECMoxygenUptakeRate << endl << endl;

 OutputFile << "QuiescenceOxygenLevel = " << QuiescenceOxygenLevel << endl;
 OutputFile << "NecrosisOxygenLevel = " << NecrosisOxygenLevel << endl;
// OutputFile << "MaxOxygenDeliveryRate = " << MaxOxygenDeliveryRate << endl << endl;
 
 OutputFile << "ApoptosisRate = " << ApoptosisRate << endl;
 OutputFile << "TumorAggressivenessG = " << TumorAggressivenessG << endl;
 OutputFile << "TumorNecrosisRateGN = " << TumorNecrosisRateGN << endl << endl;
 
 OutputFile << "time = " << t << endl << endl;

 OutputFile.close();
 
 return true;
}

bool SaveTumorData( double time )
{
 extern bool MinimalSaveMode;

 extern char SaveDirectory [1025];
 extern char* StringBuffer;
 extern char TumorParameterFile [1025];
 
 extern Matrix Phi;
 extern Matrix PhiNecrotic;
 extern Matrix PhiQuiescent;
 extern Matrix X;
 extern Matrix Y;
 
 extern Matrix Oxygen;
 extern Matrix OxygenDiffusivity;
 extern Matrix Pressure;
 extern Matrix Mobility;
 extern Matrix TAF;
 extern Matrix MDE;
 extern Matrix ECM;
 extern Matrix MDEdiffusivity;
 extern Matrix PressureGradientX;
 extern Matrix PressureGradientY;
 extern Matrix BloodVesselIndicator;
 extern Matrix BloodVesselRadius;
 extern Matrix Flow;
 extern Matrix SproutTips;
 extern Matrix VesselCells;
 extern Matrix MapIndicator;
 extern Matrix MapVesselCells;
 extern Matrix MapSproutTips;
 extern Matrix MapFlow;
 extern Matrix MapRadius;
 extern Matrix MapPressure;
 extern Matrix VEGFR;
 extern Matrix Notch;
 extern Matrix Delta4;
 extern Matrix Structure;
 extern Matrix Speed;
 
 extern Matrix HaematocritLevel;
/* 
 ConstructFilename( StringBuffer, SaveDirectory,"Phi",time,"dat" );   
 Phi.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"PhiN",time,"dat" );   
 PhiNecrotic.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"PhiQ",time,"dat" );   
 PhiQuiescent.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"X",time,"dat" );   
 X.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"Y",time,"dat" );   
 Y.WriteToFile( StringBuffer );
 
 ConstructFilename( StringBuffer, SaveDirectory,"Oxygen",time,"dat" );   
 Oxygen.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"Pressure",time,"dat" );   
 Pressure.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"TAF",time,"dat" );   
 TAF.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"MDE",time,"dat" );   
 MDE.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"ECM",time,"dat" );   
 ECM.WriteToFile( StringBuffer );   
 
 ConstructFilename( StringBuffer, SaveDirectory,"Mobility",time,"dat" );   
 Mobility.WriteToFile( StringBuffer );
 ConstructFilename( StringBuffer, SaveDirectory,"Structure",time,"dat" );   
 Structure.WriteToFile( StringBuffer );   
*/

/* 
 if( !MinimalSaveMode )
 {
  ConstructFilename( StringBuffer, SaveDirectory,"OxygenDiffusivity",time,"dat" );   
  OxygenDiffusivity.WriteToFile( StringBuffer );
  ConstructFilename( StringBuffer, SaveDirectory,"MDEdiffusivity",time,"dat" );   
  MDEdiffusivity.WriteToFile( StringBuffer );   
  ConstructFilename( StringBuffer, SaveDirectory,"PX",time,"dat" );   
  PressureGradientX.WriteToFile( StringBuffer );   
  ConstructFilename( StringBuffer, SaveDirectory,"PY",time,"dat" );   
  PressureGradientY.WriteToFile( StringBuffer ); 
  ConstructFilename( StringBuffer, SaveDirectory,"Speed",time,"dat" );   
  Speed.WriteToFile( StringBuffer );
 }
*/ 
 char CharTemp1 [1024];
 ConstructFilename( CharTemp1, SaveDirectory,"Curvature",time,"dat" );   
 char CharTemp2 [1024];
 ConstructFilename( CharTemp2, SaveDirectory,"NormalX",time,"dat" );   
 char CharTemp3 [1024];
 ConstructFilename( CharTemp3, SaveDirectory,"NormalY",time,"dat" );   

/*
 ConstructFilename( StringBuffer, SaveDirectory, "BloodVesselIndicator",time,"dat" );
 BloodVesselIndicator.WriteToFile( StringBuffer ) ;
 ConstructFilename( StringBuffer, SaveDirectory, "SproutTips",time,"dat" );
 SproutTips.WriteToFile( StringBuffer ); 
*/

/*
 if( !MinimalSaveMode ) 
 {
  ConstructFilename( StringBuffer, SaveDirectory, "BloodVesselRadius",time,"dat" );
  BloodVesselRadius.WriteToFile( StringBuffer );
  ConstructFilename( StringBuffer, SaveDirectory, "Flow",time,"dat" );
  Flow.WriteToFile( StringBuffer );
 }
*/
 
 OutputGeometricData( CharTemp2,CharTemp3,CharTemp1);
 return true;
}

bool SaveResumeTumorData( void )
{
 extern char Xfile [1025];
 extern char Yfile [1025];
 extern char PhiFile [1025];
 extern char PhiNecroticFile [1025];
 extern char PhiQuiescentFile [1025];
 extern char OxygenFile [1025];
 extern char OxygenDiffusivityFile [1025];
 extern char PressureFile [1025];
 extern char MobilityFile [1025];
 extern char ECM_File [1025];
 extern char MDEfile [1025];
 extern char TAFfile [1025];
 extern char TumorParameterFile [1025];
 extern char BloodVesselIndicatorFile [1025];
 extern char SproutTipsFile [1025];
 extern char StructureFile [1025];

 extern char BloodVesselDensityFile [1025];
 extern Matrix BloodVesselDensity;

 extern Matrix X;
 extern Matrix Y;
 extern Matrix Phi;
 extern Matrix PhiNecrotic;
 extern Matrix PhiQuiescent;
 
 extern Matrix Oxygen;
 extern Matrix OxygenDiffusivity;
 
 extern Matrix Pressure;
 extern Matrix Mobility;
 extern Matrix ECM;
 extern Matrix MDE;
 extern Matrix TAF;
 extern Matrix Structure;
 extern Matrix BloodVesselIndicator;
 extern Matrix SproutTips;
 extern Matrix VesselCells;
 extern Matrix MapIndicator;
 extern Matrix MapVesselCells;
 extern Matrix MapSproutTips;
 extern Matrix MapFlow;
 extern Matrix MapRadius;
 extern Matrix MapPressure;
 extern Matrix VEGFR;
 extern Matrix Notch;
 extern Matrix Delta4;

/*
 X.WriteToFile(Xfile);
 Y.WriteToFile(Yfile);
 
 Phi.WriteToFile(PhiFile);
 PhiNecrotic.WriteToFile(PhiNecroticFile);
 PhiQuiescent.WriteToFile(PhiQuiescentFile);
 
 Oxygen.WriteToFile(OxygenFile);
 OxygenDiffusivity.WriteToFile(OxygenDiffusivityFile);
 
 Pressure.WriteToFile(PressureFile);
 Mobility.WriteToFile(MobilityFile);
 
 ECM.WriteToFile(ECM_File);
 MDE.WriteToFile(MDEfile);
 TAF.WriteToFile(TAFfile);
 
 Structure.WriteToFile(StructureFile);
 
 BloodVesselIndicator.WriteToFile( BloodVesselIndicatorFile ) ;
 SproutTips.WriteToFile( SproutTipsFile );

 BloodVesselDensity.WriteToFile( BloodVesselDensityFile ) ;
*/ 
 return true;
}
